home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / asm / utils / crossword / includes / cwformat.i < prev    next >
Encoding:
Text File  |  1980-01-04  |  10.6 KB  |  556 lines

  1.  
  2.  
  3. * Disc Formatting code for CrossWord V1.2.
  4. * Includes several functions ripped off
  5. * from exec_support.i.
  6.  
  7.  
  8. * NewList(list,type)
  9. * a0 = list (to initialise)
  10. * d0 = type
  11.  
  12. * NON-MODIFIABLE.
  13.  
  14.  
  15. NewList        move.l    a0,(a0)        ;lh_head points to lh_tail
  16.         addq.l    #4,(a0)
  17.         clr.l    4(a0)        ;lh_tail = NULL
  18.         move.l    a0,8(a0)        lh_tailpred points to lh_head
  19.  
  20.         move.b    d0,12(a0) ;list type
  21.  
  22.         rts
  23.  
  24.  
  25. * port = CreatePort(Name,Pri)
  26. * a0 = name
  27. * d0 = pri
  28. * returns d0 = port, NULL if couldn't do it
  29.  
  30. * d1/d7/a1 corrupt
  31.  
  32. * NON-MODIFIABLE.
  33.  
  34.  
  35. CreatePort    movem.l    d0/a0,-(sp)    ;save parameters
  36.         moveq    #-1,d0
  37.         CALLEXEC    AllocSignal    ;get a signal bit
  38.         tst.l    d0
  39.         bmi    cp_error1
  40.         move.l    d0,d7        ;save signal bit
  41.  
  42.  
  43. * got signal bit. Now create port structure.
  44.  
  45.  
  46.         move.l    #mp_sizeof,d0
  47.         move.l    #MEMF_PUBLIC+MEMF_CLEAR,d1
  48.         CALLEXEC    AllocMem
  49.         tst.l    d0
  50.         beq.s    cp_error2    ;couldn't create port struct!
  51.  
  52.  
  53. * Here initialise port node structure.
  54.  
  55.  
  56.         move.l    d0,a0
  57.         movem.l    (sp)+,d0/d1    ;get parms off stack
  58.         move.l    d1,ln_Name(a0)    ;set name pointer
  59.         move.b    d0,ln_Pri(a0)    ;and priority
  60.  
  61.         move.b    #NT_MSGPORT,ln_Type(a0)    ;ensure it's a message
  62.                         ;port
  63.  
  64. * Here initialise rest of port.
  65.  
  66.  
  67.         move.b    #PA_SIGNAL,mp_Flags(a0)    ;signal if msg received
  68.         move.b    d7,mp_SigBit(a0)        ;signal bit here
  69.         move.l    a0,-(sp)
  70.         sub.l    a1,a1
  71.         CALLEXEC    FindTask        ;find THIS task
  72.         move.l    (sp)+,a0
  73.         move.l    d0,mp_SigTask(a0)    ;signal THIS task if msg arrived
  74.  
  75.  
  76. * Here, if public port, add to public port list, else
  77. * initialise message list header.
  78.  
  79.  
  80.         tst.l    ln_Name(a0)    ;got a name?
  81.         beq.s    cp_private    ;no
  82.  
  83.         move.l    a0,-(sp)
  84.         move.l    a0,a1
  85.         CALLEXEC    AddPort        ;else add to public port list
  86.         move.l    (sp)+,d0        ;(which also NewList()s the
  87.         rts            ;mp_MsgList)
  88.  
  89.  
  90. * Here initialise list header for a private port.
  91.  
  92.  
  93. cp_private    lea    mp_MsgList(a0),a1    ;ptr to list structure
  94.         exg    a0,a1        ;for now
  95.         move.b    #NT_MESSAGE,d0    ;type = message list
  96.         bsr    NewList        ;do it!
  97.  
  98.         move.l    a1,d0        ;return ptr to port
  99.         rts
  100.  
  101.  
  102. * Here couldn't allocate. Release signal bit.
  103.  
  104.  
  105. cp_error2    move.l    d7,d0
  106.         CALLEXEC    FreeSignal
  107.  
  108.  
  109. * Here couldn't get a signal so quit NOW.
  110.  
  111.  
  112. cp_error1    movem.l    (sp)+,d0/a0
  113.         moveq    #0,d0        ;signal no port exists!
  114.  
  115.         rts
  116.  
  117.  
  118. * DeletePort(Port)
  119. * a0 = port
  120.  
  121. * a1 corrupt
  122.  
  123. * NON-MODIFIABLE.
  124.  
  125.  
  126. DeletePort    move.l    a0,-(sp)
  127.         tst.l    ln_Name(a0)    ;public port?
  128.         beq.s    dp_private    ;no
  129.  
  130.         move.l    a0,a1
  131.         CALLEXEC    RemPort        ;remove port
  132.  
  133.  
  134. * here make it difficult to re-use the port.
  135.  
  136.  
  137. dp_private    move.l    (sp)+,a0
  138.         moveq    #-1,d0
  139.         move.l    d0,mp_SigTask(a0)
  140.         move.l    d0,mp_MsgList(a0)
  141.  
  142.  
  143. * Now free the signal.
  144.  
  145.  
  146.         moveq    #0,d0
  147.         move.b    mp_SigBit(a0),d0
  148.         CALLEXEC    FreeSignal
  149.  
  150.  
  151. * Now free the port structure.
  152.  
  153.  
  154.         move.l    a0,a1
  155.         move.l    #mp_sizeof,d0
  156.         CALLEXEC    FreeMem
  157.  
  158.         rts
  159.  
  160.  
  161. * IOReq=CreateExtIO(Port,Size)
  162. * a0 = port
  163. * d0 = size of IOReq to create
  164. *    (e.g., iotd_sizeof for a
  165. *    trackdisk.device IOreq)
  166.  
  167. * return d0=IOReq or NULL if couldn't do it
  168.  
  169. * Usage:call CreatePort() first to get a port
  170. * to link to the IORequest. Then call this
  171. * function to get the IORequest, passing the
  172. * port pointer in a0.
  173.  
  174.  
  175. CreateExtIO    movem.l    d0/a0,-(sp)    ;save parameters
  176.  
  177.  
  178. * Allocate the memory for the IORequest
  179.  
  180.  
  181.         move.l    #MEMF_PUBLIC+MEMF_CLEAR,d1
  182.         CALLEXEC    AllocMem
  183.         tst.l    d0
  184.         beq.s    cei_error1
  185.         move.l    d0,a1        ;pointer to IORequest
  186.  
  187.         movem.l    (sp)+,d0/a0    ;recover port & size
  188.  
  189.         move.b    #NT_MESSAGE,ln_Type(a1)
  190.         move.l    a0,mn_ReplyPort(a1)    ;set port pointer
  191.  
  192. ;        sub.l    #mn_sizeof,d0    ;leave this in for upgrades!
  193.  
  194.         move.w    d0,mn_Length(a1)        ;and struct size
  195.  
  196.         move.l    a1,d0        ;return argument
  197.         rts
  198.  
  199.  
  200. * Here couldn't get memory for IORequest, so bye
  201.  
  202.  
  203. cei_error1    movem.l    (sp)+,d0/a0
  204.         moveq    #0,d0
  205.         rts
  206.  
  207.  
  208. * DeleteExtIO(IORequest)
  209.  
  210. * a1 = IORequest
  211.  
  212. * Deletes an IORequest structure formed by CreateExtIO()
  213. * uses mn_Length field to determine how much memory to
  214. * deallocate
  215.  
  216. * d0/a1 corrupt
  217.  
  218.  
  219. DeleteExtIO    nop
  220.  
  221. ;        move.l    a1,-(sp)
  222. ;        CALLEXEC    WaitIO        ;ensure no pending requests!
  223. ;        move.l    (sp)+,a1
  224.  
  225.         moveq    #0,d0
  226.         move.w    mn_Length(a1),d0
  227.  
  228. ;        add.l    #mn_sizeof,d0    ;keep this for now!
  229.  
  230.         CALLEXEC    FreeMem
  231.  
  232.         rts
  233.  
  234.  
  235.  
  236. * DoFormat(a6)
  237. * a6 = ptr to main program variables
  238.  
  239. * d0-d2/d7/a0-a2 corrupt
  240.  
  241.  
  242. DoFormat        CALLEXEC    Forbid
  243.  
  244.         moveq    #0,d0
  245.  
  246.         move.l    d0,fmt_port(a6)    ;initialise this lot
  247.         move.l    d0,fmt_ioreq(a6)
  248.         move.l    d0,fmt_trbuf(a6)
  249.  
  250.         lea    fmt_pname(pc),a0    ;pointer to port name
  251.         moveq    #10,d0        ;priority
  252.         bsr    CreatePort    ;get a port
  253.         move.l    d0,fmt_port(a6)    ;save pointer
  254.         bne.s    DoFmt_1        ;and skip if it exists
  255.  
  256.         moveq    #13,d0        ;can't create port error
  257.         bra    DoFmt_7
  258.  
  259. DoFmt_1        move.l    d0,a0        ;ptr to port
  260.         moveq    #iotd_sizeof,d0    ;size of ioreq
  261.         bsr    CreateExtIO    ;build it
  262.         move.l    d0,fmt_ioreq(a6)    ;save ptr
  263.         bne.s    DoFmt_2        ;and skip if all's well
  264.  
  265.         moveq    #14,d0        ;can't create ioreq error
  266.         bra    DoFmt_7
  267.  
  268. DoFmt_2        lea    trd_name(pc),a0    ;trackdisk device
  269.         moveq    #0,d0        ;unit 0
  270.         move.l    fmt_ioreq(a6),a1
  271.         moveq    #0,d1
  272.  
  273.         CALLEXEC    OpenDevice    ;have we got it?
  274.         tst.l    d0
  275.         beq.s    DoFmt_3        ;yes
  276.  
  277.         moveq    #15,d0        ;can't open device error
  278.         bra    DoFmt_7
  279.  
  280. DoFmt_3        move.l    #512*22,d0
  281.         move.l    #MEMF_CHIP,d1    ;allocate track buffer
  282.         CALLEXEC    AllocMem
  283.         move.l    d0,fmt_trbuf(a6)    ;got one?
  284.         bne.s    DoFmt_4        ;skip if so
  285.  
  286.         moveq    #-1,d0        ;can't alloc trackbuf error
  287.         bra    DoFmt_7
  288.  
  289. DoFmt_4        move.l    d0,a0        ;ptr to track buf
  290.         move.l    #512*22/4,d0    ;no of longwords
  291.         move.l    #"DOS ",d1
  292.         clr.b    d1        ;initialising data
  293.  
  294. DoFmt_5        move.l    d1,(a0)+        ;pre-create track data
  295.         addq.b    #1,d1        ;to write to disc
  296.         subq.l    #1,d0
  297.         bne.s    DoFmt_5
  298.  
  299. DoFmt_4b        move.l    fmt_ioreq(a6),a1
  300.         move.w    #14,io_Command(a1)    ;get change stat
  301.         CALLEXEC    DoIO
  302.  
  303.         move.l    fmt_ioreq(a6),a1    ;wait until a disc is
  304.         tst.l    ioext_Actual(a1)    ;in the drive
  305.         bne.s    DoFmt_4b
  306.  
  307.         move.l    fmt_ioreq(a6),a1
  308.         move.w    #13,io_Command(a1)    ;get change num
  309.         CALLEXEC    DoIO
  310.  
  311.         move.l    fmt_ioreq(a6),a1
  312.         move.l    ioext_Actual(a1),d7    ;this is change num
  313.         addq.l    #2,d7            ;look for this
  314.  
  315. DoFmt_4a        move.l    fmt_ioreq(a6),a1
  316.         move.w    #13,io_Command(a1)    ;get change num
  317.         CALLEXEC    DoIO
  318.  
  319.         move.l    fmt_ioreq(a6),a1
  320.         move.l    ioext_Actual(a1),d0    ;changed disc?
  321.         cmp.l    d7,d0
  322.         bcs.s    DoFmt_4a        ;loop back until changed
  323.  
  324.  
  325. * TMP note : TD_PROTSTATUS io_Actual=0 if disc writeable, nonzero if WProtect
  326.  
  327.  
  328.         move.l    fmt_ioreq(a6),a1    ;check if write protected
  329.  
  330.         move.w    #15,io_Command(a1)
  331.         CALLEXEC    DoIO
  332.  
  333.         move.l    fmt_ioreq(a6),a0
  334.         move.l    ioext_Actual(a0),d0    ;write protected?
  335.         beq.s    DoFmt_4c            ;continue if not
  336.  
  337.         moveq    #8,d0
  338.         bra    DoFmt_7
  339.  
  340. DoFmt_4c        moveq    #0,d0
  341.         move.l    d0,fmt_offset(a6)
  342.  
  343.         move.l    fmt_ioreq(a6),a1
  344.         move.w    #9,io_Command(a1)        ;TD_MOTOR
  345.         moveq    #1,d0            ;turn it on
  346.         move.l    d0,ioext_Length(a1)
  347.  
  348.         CALLEXEC    DoIO        ;wait for completion
  349.  
  350.         move.l    #512*22,d7    ;no of bytes in a track
  351.         moveq    #80,d6        ;no of tracks to do
  352.  
  353. DoFmt_6        move.l    fmt_ioreq(a6),a1
  354.         move.l    fmt_trbuf(a6),d0
  355.         move.l    fmt_offset(a6),d1
  356.         move.l    d1,ioext_Offset(a1)
  357.         move.l    d0,ioext_Data(a1)        ;ptr to buffer
  358.         move.l    d7,ioext_Length(a1)    ;do a whole track
  359.         move.w    #11,io_Command(a1)    ;TD_FORMAT
  360.  
  361.         CALLEXEC    DoIO            ;execute it
  362.  
  363.         add.l    d7,fmt_offset(a6)
  364.  
  365.         subq.l    #1,d6            ;done all tracks?
  366.         bne.s    DoFmt_6            ;skip if so
  367.  
  368.         move.w    #128,d0        ;prepare to create the
  369.         move.l    fmt_trbuf(a6),a0    ;Root Block
  370.         move.l    a0,a1
  371.  
  372.         moveq    #2,d1
  373.         move.l    d1,(a0)+        ;type T.SHORT
  374.  
  375.         clr.l    (a0)+
  376.         clr.l    (a0)+
  377.         moveq    #$48,d1
  378.         move.l    d1,(a0)+        ;HT SIZE
  379.  
  380.         clr.l    (a0)+
  381.         clr.l    (a0)+
  382.  
  383.         moveq    #78-6,d2        ;clear hash table
  384. DoFmt_6a        clr.l    (a0)+
  385.         subq.w    #1,d2
  386.         bne.s    DoFmt_6a
  387.  
  388.         moveq    #1,d1
  389.         move.l    d1,(a0)+        ;BM Flag
  390.         move.l    #881,(a0)+    ;BM Pages
  391.  
  392.         moveq    #108-80,d2    ;clear remainder of
  393. DoFmt_6b        clr.l    (a0)+        ;BM pages
  394.         subq.w    #1,d2        ;and creation date
  395.         bne.s    DoFmt_6b
  396.  
  397.         move.l    #$0943726F,(a0)+    ;BCPL string: 9,"Cro"
  398.         move.l    #$7373776F,(a0)+    ;"sswo"
  399.         move.l    #$72640000,(a0)+    ;"rd",0,0
  400.  
  401.         moveq    #127-111,d2    ;clear remainder of
  402. DoFmt_6c        clr.l    (a0)+        ;Root Block
  403.         subq.w    #1,d2
  404.         bne.s    DoFmt_6c
  405.  
  406.         moveq    #1,d1
  407.         move.l    d1,(a0)+        ;Sec. Type = 1
  408.  
  409.         move.l    a1,a0        ;ptr to start of block
  410.  
  411.         move.w    d0,d1        ;no of longwords
  412.         moveq    #0,d2
  413.  
  414. DoFmt_6d        add.l    (a0)+,d2        ;compute checksum
  415.         subq.w    #1,d1        ;for Root Block
  416.         bne.s    DoFmt_6d
  417.  
  418.         neg.l    d2
  419.         move.l    d2,20(a1)    ;create checksum
  420.  
  421.         move.l    a0,a2        ;copy ptr to bitmap block
  422.  
  423.         clr.l    (a0)+        ;checksum for now
  424.         moveq    #-1,d2
  425.         moveq    #55,d1        ;prepare Bitmap Block
  426.  
  427. DoFmt_6e        move.l    d2,(a0)+        ;create Bitmap
  428.         subq.w    #1,d1        ;for the disc
  429.         bne.s    DoFmt_6e
  430.  
  431.         moveq    #128-56,d1
  432.  
  433. DoFmt_6f        clr.l    (a0)+        ;clear remaining Bitmap
  434.         subq.w    #1,d1        ;long words
  435.         bne.s    DoFmt_6f
  436.  
  437.         move.l    112(a2),d1    ;make blocks 880, 881
  438.         bclr    #15,d1        ;NOT free, all others
  439.         bclr    #14,d1        ;free
  440.         move.l    d1,112(a2)
  441.  
  442.         move.l    a2,a0
  443.         move.w    #128,d1
  444.         moveq    #0,d2
  445.  
  446. DoFmt_6g        add.l    (a0)+,d2        ;create Bitmap Block
  447.         subq.w    #1,d1        ;checksum
  448.         bne.s    DoFmt_6g
  449.  
  450.         neg.l    d2
  451.         move.l    d2,(a2)        ;this is it
  452.  
  453.         move.l    a1,d1            ;write out Blocks
  454.         move.l    fmt_ioreq(a6),a1        ;880, 881
  455.         move.l    d1,ioext_Data(a1)
  456.         move.l    #512*22,ioext_Length(a1)
  457.         move.l    #880*512,ioext_Offset(a1)
  458.         move.w    #11,io_Command(a1)
  459.         CALLEXEC    DoIO
  460.  
  461.         move.l    fmt_trbuf(a6),a0    ;ptr to sector to write
  462.         move.l    a0,a1        ;copy the pointer
  463.         move.w    #256,d0
  464.         move.l    #"DOS ",d1
  465.         clr.b    d1
  466.  
  467. DoFmt_6h        move.l    d1,(a0)+        ;clr for Boot Block
  468.         subq.w    #1,d0
  469.         bne.s    DoFmt_6h
  470.  
  471.         move.l    a1,a0
  472.         move.l    #880,8(a0)    ;pointer to Root Block
  473.  
  474.         move.w    #256,d0
  475.         moveq    #0,d1        ;initial checksum
  476.  
  477. DoFmt_6i        add.l    (a0)+,d1        ;create checksum
  478.         subq.w    #1,d0
  479.         bne.s    DoFmt_6i
  480.  
  481.         neg.l    d1
  482.         move.l    d1,4(a1)        ;write out checksum
  483.  
  484.         move.l    a1,d1            ;write out the
  485.         move.l    fmt_ioreq(a6),a1        ;final Boot Block
  486.         move.l    d1,ioext_Data(a1)
  487.         move.l    #512*22,ioext_Length(a1)
  488.         move.l    #0,ioext_Offset(a1)
  489.         move.w    #11,io_Command(a1)
  490.         CALLEXEC    DoIO
  491.  
  492.         move.l    fmt_ioreq(a6),a1
  493.         move.w    #9,io_Command(a1)        ;TD_MOTOR
  494.         moveq    #0,d0            ;turn it off
  495.         move.l    d0,ioext_Length(a1)
  496.  
  497.         CALLEXEC    DoIO        ;wait for completion
  498.  
  499.         moveq    #0,d0    ;signal all went well
  500.  
  501. DoFmt_7        move.w    d0,disc_error(a6)    ;save error code
  502.  
  503.         CALLEXEC    Permit
  504.  
  505.         tst.w    disc_error(a6)    ;was there an error?
  506.         bne.s    DoFmt_7_1    ;skip if so
  507.  
  508.         lea    IRT_1(pc),a0    ;else pop up the "Formatting
  509.         lea    FormatEnd(pc),a1    ;completed" panel
  510.         moveq    #3,d0
  511.  
  512.         move.l    a0,irt_itext(a6)
  513.         move.l    a1,irt_tlist(a6)
  514.         move.w    d0,irt_count(a6)
  515.  
  516.         bsr    ShowDiscInfo
  517.  
  518. DoFmt_7_1    cmp.w    #15,disc_error(a6)    ;couldn't open device?
  519.         beq.s    DoFmt_7a            ;skip if so
  520.  
  521.         move.l    fmt_ioreq(a6),a1
  522.         CALLEXEC    CloseDevice
  523.  
  524. DoFmt_7a        move.l    fmt_ioreq(a6),d0    ;got IORequest?
  525.         beq.s    DoFmt_8        ;skip if not
  526.         move.l    d0,a1
  527.         bsr    DeleteExtIO    ;else deallocate it
  528.  
  529. DoFmt_8        move.l    fmt_port(a6),d0    ;got message port?
  530.         beq.s    DoFmt_9        ;skip if not
  531.         move.l    d0,a0
  532.         bsr    DeletePort    ;else deallocate it
  533.  
  534. DoFmt_9        move.l    fmt_trbuf(a6),d0    ;got track buffer?
  535.         beq.s    DoFmt_11        ;skip if not
  536.         move.l    d0,a1
  537.         move.l    #512*22,d0
  538.         CALLEXEC    FreeMem        ;else deallocate it
  539.  
  540. DoFmt_11        move.w    disc_error(a6),d0
  541.         bra    Load_Error
  542.  
  543.  
  544. fmt_pname    dc.b    "CWFormat Port",0
  545.  
  546. trd_name        dc.b    "trackdisk.device",0
  547.  
  548. _df0        dc.b    "DF0:",0
  549.  
  550.         even
  551.  
  552.  
  553.  
  554.  
  555.  
  556.